<template>
  <div class="app-container" >
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="科室名称" prop="name">
        <el-select v-model="queryParams.code" placeholder="请选择科室名称" clearable size="small">
          <el-option
            v-for="dict in dict.type.科室"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['his:department:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['his:department:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['his:department:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['his:department:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="departmentList">
      <el-table-column label="医生名称" align="center" prop="userName" />
      <el-table-column label="门诊时间" align="center" prop="updateTime" />
      <el-table-column label="诊查费" align="center" prop="treatmentPrice" />
      <el-table-column label="限额数" align="center" prop="allowNum" />
      <el-table-column label="已挂号数" align="center" prop="nowNum"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
        <el-button
          size="medium"
          type="success"
          icon="el-icon-s-custom"
          @click="handleGH(scope.row)"
        >挂号</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />













    <!-- 添加或修改挂号收费对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="创建时间" prop="createDatetime">
          <el-input v-model="form.createDatetime" placeholder="请输入创建时间" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="科室名称" prop="name">
          <el-select v-model="form.name" placeholder="请选择科室名称">
            <el-option
              v-for="dict in dict.type.科室"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="拼音代码" prop="nameCode">
          <el-input v-model="form.nameCode" placeholder="请输入拼音代码" />
        </el-form-item>
        <el-form-item label="0代表普通门诊1代表急诊" prop="type">
          <el-select v-model="form.type" placeholder="请选择0代表普通门诊1代表急诊">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog  :visible.sync="openGH" title="挂号">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
            <el-form-item label="卡号" prop="card_id">
              <el-input v-model="formData.card_id" placeholder="请输入卡号" clearable :style="{width: '50%'}">
              </el-input>
            </el-form-item>
            <el-form-item label="挂号费(元)" prop="treatment_price">
              <el-input v-model="formData.treatment_price" readonly placeholder="请输入挂号费" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
            <el-form-item label="诊查费(元)" prop="treatment_price2">
              <el-input v-model="formData.treatment_price2" readonly placeholder="诊查费(元)" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
            <el-form-item label="付款方式" prop="pay_type">
              <el-select v-model="formData.pay_type" placeholder="请输入付款方式" clearable :style="{width: '100%'}">
                <el-option v-for="(item, index) in pay_typeOptions" :key="index" :label="item.label"
                           :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="closeGH">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>

























  </div>
</template>

<script>
import { listDepartment,selectDoctorByDepartment, gh ,getDepartment, delDepartment, addDepartment, updateDepartment, exportDepartment } from "@/api/his/department";
import { MessageBox } from 'element-ui';
export default {
  name: "Department",
  dicts: ['科室'],
  data() {
    return {
      formData: {
        card_id: undefined,
        treatment_price: '5元',
        treatment_price2: undefined,
        pay_type: undefined,
      },
      queue:{},
      pay_typeOptions: [{
        "label": "支付宝",
        "value": "支付宝"
      }, {
        "label": "现金",
        "value": "现金"
      }],
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 挂号收费表格数据
      departmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 挂号弹出层
      openGH:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: '',
      },

      // 表单参数
      form: {},
      elForm: {},
      // 表单校验
      rules: {
        createDatetime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "0代表普通门诊1代表急诊不能为空", trigger: "change" }
        ],card_id: [{
          required: true,
          message: '请输入卡号',
          trigger: 'blur'
        }],
        treatment_price: [{
          required: true,
          message: '请输入挂号费',
          trigger: 'blur'
        }],
        treatment_price2: [{
          required: true,
          message: '请输入诊查费',
          trigger: 'blur'
        }],
        pay_type: [{
          required: true,
          message: '请选择付款方式',
          trigger: 'change'
        }],
      }
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询挂号收费列表 */
    getList() {
      this.loading = true;
      listDepartment(this.queryParams).then(response => {
        this.departmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //根据科室编号查询查找医生
    s() {
      this.loading = true;
      selectDoctorByDepartment(this.queryParams.code).then(response => {
        console.log(response);
        this.departmentList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createDatetime: null,
        address: null,
        code: null,
        name: null,
        nameCode: null,
        type: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.s();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加挂号收费";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDepartment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改挂号收费";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDepartment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDepartment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //保存医生信息和病人信息
    handelConfirm(){
      console.log(this.queue);
      console.log(this.formData.card_id);

      const data = {
        cardId:this.formData.card_id,
        doctorInfor:this.queue,
        payType:this.formData.pay_type,
        code:this.queryParams.code,
        treatmentPrice:this.formData.treatment_price
      }


      this.$refs["elForm"].validate(valid => {
        if (valid) {

          gh(data).then(response => {
              console.log(response);
              //挂号成功
              if(response.msg=="挂号成功"){
                this.$message({
                  dangerouslyUseHTMLString: true,
                  message: '<strong><font style="color: #00afff;font-size: 20px">挂号成功</font></strong>'
                });
                //关闭弹窗
                this.closeGH();
                this.resetForm("elForm");
              }else {
                this.$message({
                  dangerouslyUseHTMLString: true,
                  message: '<strong><font style="color: #d73a49; font-size: 20px">'+response.msg+'!!!</font></strong>'
                });
                //关闭弹窗
                this.closeGH();
                this.resetForm("elForm");
              }
          });


        }
      });



    },
    /** 挂号按钮操作 */
    handleGH(row) {

      if(row.allowNum==row.nowNum){
        this.$alert('<strong> <font color="#7fffd4" style="font-size: 16px">'+row.userName+' 医生今日挂号数已满</font></strong>', '消息提示', {
          dangerouslyUseHTMLString: true
        })
            return
      }
      this.$alert('<strong>确认选择吗? 医生:  <font color="#7fffd4" style="font-size: 16px">'+row.userName+'</font></strong>', '消息提示', {
        dangerouslyUseHTMLString: true
      }).then(() => {
        const ids = row.id || this.ids;
        console.log(row);
        this.formData.treatment_price2 = row.treatmentPrice;
        this.queue=row;
        // 打开挂号窗口
        this.openGH = true;
      });
      /**
       *
       *    医生和病人关联起来
       *
       *
       *
       */




    },
    closeGH(){
      this.resetForm("formData");
      //关闭挂号窗口
      this.openGH = false;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有挂号收费数据项？').then(() => {
        this.exportLoading = true;
        return exportDepartment(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
